import { IRootState } from "@/store/types"
import { Module } from "vuex/types/index.js"
import { ApiDocItem, IAppState } from "./app-types"

export const ADD_ITEM = 'ADD_ITEM'
export const UPDATE_ITEM = 'UPDATE_ITEM'
export const DELETE_ITEM = 'DELETE_ITEM'
export const EDIT_ITEM = 'EDIT_ITEM'
export const SAVE_ITEM = 'SAVE_ITEM'
export const CANCEL_EDIT_ITEM = 'CANCEL_EDIT_ITEM'
export const ACTIVE_ITEM = 'ACTIVE_ITEM'
export const UPDATE_FORMDATA = 'UPDATE_FORMDATA'
export const UPDATE_PAGE_BINDING = 'UPDATE_PAGE_BINDING'

const app: Module<IAppState, IRootState> = {
  namespaced: true,
  state: {
    items: [],
    formData: {
      templateName: undefined,
      controllersDir: undefined,
      definitionsDir: undefined,
      apiDocUrl: undefined,
    },
    activeItem: undefined,
    pageBingding: undefined
  },
  mutations: {
    [ADD_ITEM]: (state, payload: ApiDocItem) => {
      payload.isNew = true
      payload.editing = true
      payload.name = 'ITEM_'+Date.now().toFixed(0) + "_" + (Math.random() * 1000).toFixed(0).padStart(3, '0')
      payload.controllersDir = '@/controllers'
      payload.definitionsDir = '@/controllers/definitions'
      state.items.push(payload)
      return state
    },
    [UPDATE_ITEM]: (state, payload: ApiDocItem) => {
      var item = state.items.find((item) => item.name == payload.name && item.editing === true)
      item.title = payload.title
      item.url = payload.url
      item.editing = false
      return state
    },
    [DELETE_ITEM]: (state, payload: ApiDocItem) => {
      state.items = state.items.filter(item => item.name !== payload.name)
      return state
    },
    [EDIT_ITEM]: (state, payload: ApiDocItem) => {
      var item = state.items.find((item) => item.name == payload.name && item.editing === false)
      if(item) {
        item.editing = true
      }
      return state
    },
    [SAVE_ITEM]: (state, payload: ApiDocItem) => {
      var item = state.items.find((item) => item.name == payload.name && item.editing === true)
      item.isNew = false
      item.editing = false
      return state
    },
    [CANCEL_EDIT_ITEM]:  (state, payload: ApiDocItem) => {
      var item = state.items.find((item) => item.name == payload.name && item.editing === true)
      if(item) {
        item.editing = false
        return
      }
      return state
    },
    [ACTIVE_ITEM]: (state, payload: ApiDocItem) => {
      state.activeItem = payload
      state.formData.apiDocUrl = payload.url
      state.formData.controllersDir = payload.controllersDir
      state.formData.definitionsDir = payload.definitionsDir
      state.formData = Object.assign({}, state.formData)
      return state
    },
    [UPDATE_FORMDATA]: (state, payload: {[key:  string]: any}) => {
      state.formData = Object.assign({}, state.formData, payload)
      return state
    },
    [UPDATE_PAGE_BINDING]: (state, payload: {[key:  string]: any}) => {
      state.pageBingding = Object.assign({}, state.pageBingding, payload)
      return state
    },
  },
  actions: {
    addItem: ({ commit }, payload: ApiDocItem) => {
      commit(ADD_ITEM, payload)
    },
    updateItem: ({ commit }, payload: ApiDocItem) => {
      commit(UPDATE_ITEM, payload)
    },
    deleteItem: ({ commit }, payload: ApiDocItem) => {
      commit(DELETE_ITEM, payload)
    },
    editItem: ({ commit }, payload: ApiDocItem) => {
      commit(EDIT_ITEM, payload)
    },
    saveItem: ({ commit }, payload: ApiDocItem) => {
      commit(SAVE_ITEM, payload)
    },
    cancelEditItem:  ({ commit }, payload: ApiDocItem) => {
      commit(CANCEL_EDIT_ITEM, payload)
    },
    activeItem: ({ commit }, payload: ApiDocItem) => {
      commit(ACTIVE_ITEM, payload)
    },
    updateFormData: ({ commit }, payload: string) => {
      commit(UPDATE_FORMDATA, payload)
    },
    updatePageBinding: ({ commit }, payload: string) => {
      commit(UPDATE_PAGE_BINDING, payload)
    },
  },
  getters: {}
}

export default app
